/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVQL;

import DAO.CTPNhapKhoDao;
import GUI.TOOL.ExcelOpener;
import DAO.NhaCungCapDao;
import DAO.NhanVienDao;
import DAO.PNhapKhoDao;
import DAO.SanPhamDao;
import GUI.TOOL.XuatExcel;
import GUI.Var;
import POJO.CTPNhapKho;
import POJO.NhaCungCap;
import POJO.NhanVien;
import POJO.PNhapKho;
import POJO.SanPham;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DropMode;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Hồng
 */
public class frXemPhieuNhapKho extends javax.swing.JInternalFrame {

	private DefaultComboBoxModel cbModel;
	private DefaultTableModel model = null;
	private DefaultTableModel model2 = null;
	private DefaultTableModel modelrong;
	private int SD;
	private int TSD;// tong so dong trong bang hoa don
	private int THT; //trang hiện tại

	private void loadCbNV() {

		NhanVienDao dao = new NhanVienDao();

		ArrayList<NhanVien> ds = dao.layDanhSach();
		NhanVien nv = new NhanVien(0, "ALL", "", "", 0, 0);
		this.cbModel.addElement(nv);
		for (int i = 0; i < ds.size(); i++) {
			this.cbModel.addElement(ds.get(i));

		}
		if (cbModel.getSize() > 0) {
			cbModel.setSelectedItem(cbModel.getElementAt(0));
		}
	}

	public void DefaultFrom() {
		model2 = new DefaultTableModel();
		this.model2.addColumn("Số PN");
		this.model2.addColumn("Ngày nhập");
		this.model2.addColumn("NV phụ trách");

		model = new DefaultTableModel();
		this.model.addColumn("Mã sản phẩm");
		this.model.addColumn("Ngày");
		this.model.addColumn("XX");
		this.model.addColumn("YY");
	}

	private void load(int soDong, int trangSo) {
		int manv = jcbNV.getSelectedIndex();
		if (manv == 0) {
			loadAll(SD, THT);

		} else {
			ArrayList<PNhapKho> dsPNhapKho = null;

			try {

				dsPNhapKho = PNhapKhoDao.layDanhSachByNhanVienPhanTrang(manv, soDong, trangSo);
				//dsPNhapKho= PNhapKhoDao.layDanhSach();

			} catch (Exception e) {
			}

			NhaCungCapDao dao = new NhaCungCapDao();
			NhanVienDao nvdao = new NhanVienDao();
			DefaultFrom();
			//int stt = 1;
			for (PNhapKho pnk : dsPNhapKho) {
				manv = pnk.getMaNVPhuTrach();
				NhanVien nv = nvdao.layByMa(manv);
				Object[] items = new Object[]{
					pnk.getSoPNK(),
					Var.hienThiNgayVN(pnk.getNgayNhap()),
					//pnk.getMaNVPhuTrach()
					nv.getTenNV(),
					pnk.getMaNCC()
				};
				Object[] items1 = new Object[]{
					pnk.getSoPNK(),
					Var.hienThiNgayVN(pnk.getNgayNhap()),
					pnk.getMaNCC(),
					pnk.getMaNVPhuTrach()};
				model.addRow(items1);
				model2.addRow(items);
				//stt++;

			}
			jtbPhieuNhap.setModel(model2);

			int[] va = {20, 20, 60};
			int[] pt = new int[3];
			for (int l = 0; l < 3; l++) {
				pt[l] = jtbPhieuNhap.getWidth() * va[l] / 100;
				jtbPhieuNhap.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

			}
		}
	}

	private void loadAll(int soDong, int trangSo) {
		ArrayList<PNhapKho> dsPNhapKho = PNhapKhoDao.layDanhSachPhanTrang(soDong, trangSo);
		String[] columns = new String[]{"Mã phiếu nhập", "Ngày nhập", "NV phụ trách"};

		//DefaultTableModel model2 = new DefaultTableModel(null, columns);
		NhanVienDao nvdao = new NhanVienDao();
		DefaultFrom();
		//int stt = 1;
		for (PNhapKho pnk : dsPNhapKho) {
			int manv = pnk.getMaNVPhuTrach();
			NhanVien nv = nvdao.layByMa(manv);
			Object[] items = new Object[]{
				pnk.getSoPNK(),
				Var.hienThiNgayVN(pnk.getNgayNhap()),
				//cc.getTenNCC()
				//pnk.getMaNVPhuTrach()
				nv.getTenNV(),
				pnk.getMaNCC()
			};
			Object[] items1 = new Object[]{
				pnk.getSoPNK(),
				Var.hienThiNgayVN(pnk.getNgayNhap()),
				pnk.getMaNCC(),
				pnk.getMaNVPhuTrach()};
			model.addRow(items1);
			model2.addRow(items);
			//stt++;
		}
		jtbPhieuNhap.setModel(model2);
		int[] va = {20, 20, 60};
		int[] pt = new int[3];
		for (int l = 0; l < 3; l++) {
			pt[l] = jtbPhieuNhap.getWidth() * va[l] / 100;
			jtbPhieuNhap.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}
	}

	public void loadCT(int dong) {
		// load tren text
		int soPNK = (Integer) model.getValueAt(dong, 0);
		String NgayNhap = (String) model.getValueAt(dong, 1);
		int maNCC = (Integer) model.getValueAt(dong, 2);
		int maNVPhuTrach = (Integer) model.getValueAt(dong, 3);
		NhaCungCapDao ncc = new NhaCungCapDao();
		NhaCungCap dsncc = ncc.layByMa(maNCC);
		NhanVienDao nv = new NhanVienDao();
		NhanVien dsnv = nv.layByMa(maNVPhuTrach);

		jtxtMaPN.setText(String.valueOf(soPNK));
		jtxtNgayNhap.setText(NgayNhap);
		jtxtNCC.setText(dsncc.getTenNCC());
		jtxtNV.setText(dsnv.getTenNV());

		// load luoi chi tiet 

		SanPhamDao dao = new SanPhamDao();
		SanPham sp = new SanPham();
		ArrayList<CTPNhapKho> dsCTPNhapKho = CTPNhapKhoDao.layDanhSachBySoPNK(soPNK);
		String[] columns = new String[]{"STT", "Tên sản phẩm", "Số lượng", "Giá nhập", "Thành tiền"};
		DefaultTableModel model = new DefaultTableModel(null, columns);
		int t = 1;
		double gia = 0;
		for (CTPNhapKho ctpnk : dsCTPNhapKho) {
			sp = dao.layDanhSachByMaSP(ctpnk.getMaSP());
			Object[] items = new Object[]{
				t,
				//ctpnk.getMaSP(),
				sp.getTenSP(),
				ctpnk.getSoLuong(),
				ctpnk.getGiaNhap(),
				ctpnk.getSoLuong() * ctpnk.getGiaNhap()
			};
			t++;
			model.addRow(items);
			gia += Double.valueOf(ctpnk.getGiaNhap());
		}
		jtxtTriGia.setText(Double.toString(gia));
		jtbCTPhieuNhap.setModel(model);
		int[] va = {10, 10, 30, 25, 25};
		int[] pt = new int[5];
		for (int l = 0; l < 5; l++) {
			pt[l] = jtbCTPhieuNhap.getWidth() * va[l] / 100;
			jtbCTPhieuNhap.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}
	}

	/**
	 * Creates new form frXemPhieuNhapKho
	 */
	public frXemPhieuNhapKho() {
		initComponents();
		TSD = soDongDuLieu();
		THT = 0;

		this.cbModel = new DefaultComboBoxModel();
		this.jcbNV.setModel(cbModel);
		loadCbNV();

		SD = 20;
		loadAll(SD, THT);
		load(SD, THT);
//                if (model.getRowCount() > 0) {			
//			loadCT(0);
//		}

		setTxt();

		jtxtMaPN.setText(null);
		jtxtNCC.setText(null);
		jtxtNV.setText(null);
		jtxtNgayNhap.setText(null);
		jtxtTriGia.setText(null);
	}

	private void setTxt() {
		lbPhanTrang.setText(Integer.toString(THT + 1) + "/" + Integer.toString(TSD / SD + 1));
	}

	private int soDongDuLieu() {
		int tmp = 0;
		PNhapKhoDao dao = new PNhapKhoDao();
		try {
			tmp = dao.laySoDong();
		} catch (Exception e) {
		}
		return tmp;
	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel3 = new javax.swing.JPanel();
              jPanel4 = new javax.swing.JPanel();
              jScrollPane2 = new javax.swing.JScrollPane();
              jtbPhieuNhap = new javax.swing.JTable();
              jLabel7 = new javax.swing.JLabel();
              jcbNV = new javax.swing.JComboBox();
              jPanel11 = new javax.swing.JPanel();
              jButton10 = new javax.swing.JButton();
              jButton11 = new javax.swing.JButton();
              jButton12 = new javax.swing.JButton();
              jButton13 = new javax.swing.JButton();
              lbPhanTrang = new javax.swing.JLabel();
              jPanel6 = new javax.swing.JPanel();
              jPanel8 = new javax.swing.JPanel();
              jLabel2 = new javax.swing.JLabel();
              jtxtMaPN = new javax.swing.JTextField();
              jLabel3 = new javax.swing.JLabel();
              jtxtNgayNhap = new javax.swing.JTextField();
              jLabel4 = new javax.swing.JLabel();
              jtxtNCC = new javax.swing.JTextField();
              jLabel5 = new javax.swing.JLabel();
              jtxtNV = new javax.swing.JTextField();
              jLabel6 = new javax.swing.JLabel();
              jtxtTriGia = new javax.swing.JTextField();
              jLabel11 = new javax.swing.JLabel();
              jPanel7 = new javax.swing.JPanel();
              jScrollPane1 = new javax.swing.JScrollPane();
              jtbCTPhieuNhap = new javax.swing.JTable();
              jPanel1 = new javax.swing.JPanel();
              jLabel1 = new javax.swing.JLabel();
              jPanel5 = new javax.swing.JPanel();
              jbtnLapBaoCao = new javax.swing.JButton();
              jbtExcel = new javax.swing.JButton();
              jLabel8 = new javax.swing.JLabel();
              lbTSSPDX = new javax.swing.JLabel();
              jLabel9 = new javax.swing.JLabel();
              lbTongSoLuong = new javax.swing.JLabel();
              jLabel10 = new javax.swing.JLabel();
              jlbTSLanXuatKho = new javax.swing.JLabel();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách phiếu nhập"));
              jPanel4.setToolTipText("");

              jtbPhieuNhap.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "Số PN", "Ngày nhập", "NV phụ trách"
                     }
              ));
              jtbPhieuNhap.addMouseListener(new java.awt.event.MouseAdapter() {
                     public void mouseClicked(java.awt.event.MouseEvent evt) {
                            jtbPhieuNhapMouseClicked(evt);
                     }
              });
              jScrollPane2.setViewportView(jtbPhieuNhap);

              jLabel7.setText("Chọn nhân viên:");

              jcbNV.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ALL", "Item 2", "Item 3", "Item 4" }));
              jcbNV.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jcbNVActionPerformed(evt);
                     }
              });

              jButton10.setText("|<");
              jButton10.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton10ActionPerformed(evt);
                     }
              });

              jButton11.setText("<");
              jButton11.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton11ActionPerformed(evt);
                     }
              });

              jButton12.setText(">|");
              jButton12.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton12ActionPerformed(evt);
                     }
              });

              jButton13.setText(">");
              jButton13.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton13ActionPerformed(evt);
                     }
              });

              lbPhanTrang.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              lbPhanTrang.setText("0");

              javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
              jPanel11.setLayout(jPanel11Layout);
              jPanel11Layout.setHorizontalGroup(
                     jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel11Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton10)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton11)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lbPhanTrang)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton13)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton12)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel11Layout.setVerticalGroup(
                     jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel11Layout.createSequentialGroup()
                            .addGap(0, 0, Short.MAX_VALUE)
                            .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jButton10)
                                   .addComponent(jButton11)
                                   .addComponent(jButton12)
                                   .addComponent(jButton13)
                                   .addComponent(lbPhanTrang)))
              );

              javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
              jPanel4.setLayout(jPanel4Layout);
              jPanel4Layout.setHorizontalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel7)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jcbNV, 0, 205, Short.MAX_VALUE)
                            .addContainerGap())
                     .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                     .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );
              jPanel4Layout.setVerticalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel7)
                                   .addComponent(jcbNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
              );

              jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin phiếu nhập kho"));

              jLabel2.setText("Số phiếu nhập:");

              jtxtMaPN.setEditable(false);

              jLabel3.setText("Ngày nhập:");

              jtxtNgayNhap.setEditable(false);

              jLabel4.setText("Nhà cung cấp:");

              jtxtNCC.setEditable(false);

              jLabel5.setText("Nhân viên phụ trách:");

              jtxtNV.setEditable(false);

              jLabel6.setText("Trị giá phiếu nhập:");

              jtxtTriGia.setEditable(false);

              jLabel11.setText("VNĐ");

              javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
              jPanel8.setLayout(jPanel8Layout);
              jPanel8Layout.setHorizontalGroup(
                     jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel8Layout.createSequentialGroup()
                            .addGap(12, 12, 12)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addComponent(jtxtNV, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jtxtNCC, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                                   .addComponent(jtxtNgayNhap)
                                   .addComponent(jtxtMaPN)
                                   .addComponent(jtxtTriGia))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel11)
                            .addContainerGap())
              );
              jPanel8Layout.setVerticalGroup(
                     jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel8Layout.createSequentialGroup()
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel2)
                                   .addComponent(jtxtMaPN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel3)
                                   .addComponent(jtxtNgayNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel4)
                                   .addComponent(jtxtNCC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel5)
                                   .addComponent(jtxtNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel6)
                                   .addComponent(jtxtTriGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel11))
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Chi tiết phiếu nhập kho"));

              jtbCTPhieuNhap.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Sản phẩm", "Số lượng", "Giá nhập", "Thành tiền"
                     }
              ));
              jScrollPane1.setViewportView(jtbCTPhieuNhap);

              javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
              jPanel7.setLayout(jPanel7Layout);
              jPanel7Layout.setHorizontalGroup(
                     jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
              );
              jPanel7Layout.setVerticalGroup(
                     jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
              );

              javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
              jPanel6.setLayout(jPanel6Layout);
              jPanel6Layout.setHorizontalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );
              jPanel6Layout.setVerticalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel6Layout.createSequentialGroup()
                            .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
              jPanel3.setLayout(jPanel3Layout);
              jPanel3Layout.setHorizontalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel3Layout.setVerticalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );

              jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel1.setText("XEM PHIẾU NHẬP KHO");

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Chức năng"));

              jbtnLapBaoCao.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jbtnLapBaoCao.setText("Lập báo cáo");
              jbtnLapBaoCao.setToolTipText("gọi lập phiếu nhập kho");
              jbtnLapBaoCao.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnLapBaoCaoActionPerformed(evt);
                     }
              });

              jbtExcel.setText("Lưu ra Excel");
              jbtExcel.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtExcelActionPerformed(evt);
                     }
              });

              jLabel8.setText("Tổng số phiếu nhâp:");

              lbTSSPDX.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              lbTSSPDX.setText("0");

              jLabel9.setText("Từ ngày:");

              lbTongSoLuong.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              lbTongSoLuong.setText("0");

              jLabel10.setText("Đến ngày:");

              jlbTSLanXuatKho.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jlbTSLanXuatKho.setText("0");

              javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
              jPanel5.setLayout(jPanel5Layout);
              jPanel5Layout.setHorizontalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel5Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jbtnLapBaoCao)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbtExcel, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jLabel8)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lbTSSPDX)
                            .addGap(56, 56, 56)
                            .addComponent(jLabel9)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lbTongSoLuong)
                            .addGap(49, 49, 49)
                            .addComponent(jLabel10)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jlbTSLanXuatKho)
                            .addContainerGap(152, Short.MAX_VALUE))
              );
              jPanel5Layout.setVerticalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel5Layout.createSequentialGroup()
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                          .addComponent(jLabel8)
                                          .addComponent(jLabel9)
                                          .addComponent(jLabel10)
                                          .addComponent(lbTSSPDX)
                                          .addComponent(lbTongSoLuong)
                                          .addComponent(jlbTSLanXuatKho))
                                   .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                          .addComponent(jbtnLapBaoCao)
                                          .addComponent(jbtExcel)))
                            .addGap(10, 10, 10))
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, 0))
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

       private void jbtnLapBaoCaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLapBaoCaoActionPerformed
		// TODO add your handling code here:
		int soluong = jtbPhieuNhap.getRowCount();
		lbTSSPDX.setText(String.valueOf(soluong));
		String ngaybd = model.getValueAt(0, 1).toString();
		lbTongSoLuong.setText(String.valueOf(ngaybd));
		String ngaykt = model.getValueAt(Integer.valueOf(model.getRowCount()) - 1, 1).toString();
		jlbTSLanXuatKho.setText(String.valueOf(ngaykt));
       }//GEN-LAST:event_jbtnLapBaoCaoActionPerformed

       private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
		// TODO add your handling code here:
//              // ve dau tien
		THT = 0;
		load(SD, THT);
		loadAll(SD, THT);
		if (model.getRowCount() > 0) {
			//tbHoaDonSelectedRow() =0;
			loadCT(0);
		}
		setTxt();
       }//GEN-LAST:event_jButton10ActionPerformed

       private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
		// TODO add your handling code here:
		//ve trang truoc
		if (THT != 0) {
			THT -= 1;
			load(SD, THT);
			loadAll(SD, THT);
			if (model.getRowCount() > 0) {
				//tbHoaDonSelectedRow() =0;
				loadCT(0);
			}
		}
		setTxt();
       }//GEN-LAST:event_jButton11ActionPerformed

       private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
		// TODO add your handling code here:
		// ve cuoi
		THT = TSD / SD;
		load(SD, THT);
		loadAll(SD, THT);
		if (model.getRowCount() > 0) {
			//tbHoaDonSelectedRow() =0;
			loadCT(0);
		}
		setTxt();
       }//GEN-LAST:event_jButton12ActionPerformed

       private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed
		// TODO add your handling code here:
		if (THT < TSD / SD) {
			THT += 1;
			load(SD, THT);
			loadAll(SD, THT);
			if (model.getRowCount() > 0) {
				//tbHoaDonSelectedRow() =0;
				loadCT(0);
			}
		}
		setTxt();
       }//GEN-LAST:event_jButton13ActionPerformed

    private void jcbNVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbNVActionPerformed

	    load(SD, 0);
	    jtxtMaPN.setText(null);
	    jtxtNCC.setText(null);
	    jtxtNV.setText(null);
	    jtxtNgayNhap.setText(null);
	    jtxtTriGia.setText(null);
	    String[] columns = new String[]{"STT", "Tên sản phẩm", "Số lượng", "Giá nhập", "Thành tiền"};
	    DefaultTableModel model = new DefaultTableModel(null, columns);
	    jtbCTPhieuNhap.setModel(model);
    }//GEN-LAST:event_jcbNVActionPerformed

    private void jtbPhieuNhapMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbPhieuNhapMouseClicked
	    int dong = this.jtbPhieuNhap.getSelectedRow();
	    loadCT(dong);

    }//GEN-LAST:event_jtbPhieuNhapMouseClicked

    private void jbtExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtExcelActionPerformed
	    try {
		    //     Note that i'm actually saving the file first
		    XuatExcel exp = new XuatExcel();
		    File file = new File("BaoCaoNhapKho.xls");
		    exp.exportTable(jtbPhieuNhap, file);

		    ExcelOpener opn = new ExcelOpener();
		    opn.openTable(file);
	    } catch (IOException ex) {
		    System.out.println(ex.getMessage());
		    ex.printStackTrace();
	    }
    }//GEN-LAST:event_jbtExcelActionPerformed
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JButton jButton10;
       private javax.swing.JButton jButton11;
       private javax.swing.JButton jButton12;
       private javax.swing.JButton jButton13;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel10;
       private javax.swing.JLabel jLabel11;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel3;
       private javax.swing.JLabel jLabel4;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JLabel jLabel6;
       private javax.swing.JLabel jLabel7;
       private javax.swing.JLabel jLabel8;
       private javax.swing.JLabel jLabel9;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel11;
       private javax.swing.JPanel jPanel3;
       private javax.swing.JPanel jPanel4;
       private javax.swing.JPanel jPanel5;
       private javax.swing.JPanel jPanel6;
       private javax.swing.JPanel jPanel7;
       private javax.swing.JPanel jPanel8;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JScrollPane jScrollPane2;
       private javax.swing.JButton jbtExcel;
       private javax.swing.JButton jbtnLapBaoCao;
       private javax.swing.JComboBox jcbNV;
       private javax.swing.JLabel jlbTSLanXuatKho;
       private javax.swing.JTable jtbCTPhieuNhap;
       private javax.swing.JTable jtbPhieuNhap;
       private javax.swing.JTextField jtxtMaPN;
       private javax.swing.JTextField jtxtNCC;
       private javax.swing.JTextField jtxtNV;
       private javax.swing.JTextField jtxtNgayNhap;
       private javax.swing.JTextField jtxtTriGia;
       private javax.swing.JLabel lbPhanTrang;
       private javax.swing.JLabel lbTSSPDX;
       private javax.swing.JLabel lbTongSoLuong;
       // End of variables declaration//GEN-END:variables
}
